package vn.demand.dragon.model
{
	import mx.collections.XMLListCollection;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.business.LoadProjectFilesByReferenceDelegate;
	import vn.demand.dragon.model.business.RemoveProjectFileDelegate;
	import vn.demand.dragon.model.business.SaveProjectFileDelegate;
	
	public class ProjectFilesProxy extends Proxy implements IProxy
	{
		public static const NAME:String = "ProjectFilesProxy";
		
		public var errorStatus:String;
		
		private var pfiles:XMLListCollection;
		
		public static function makeId(projectVO:Object):String {
        	return NAME + projectVO.project_id + "::";
        }
		
		public function ProjectFilesProxy(proxyName:String, data:Object = null) 
        {
            super (proxyName, data );
        }
        
        public function get projectVO():Object {
        	return data as Object;
        }
        
        public function loadProjectFilesByReference():void {
			var delegate:LoadProjectFilesByReferenceDelegate = new LoadProjectFilesByReferenceDelegate(resultLoadProjectFilesByReference, failedLoadProjectFilesByReference);
			delegate.loadProjectFilesByReference(projectVO.project_id);
		}
		
		public function resultLoadProjectFilesByReference(rpcEvent:Object):void {
			pfiles = new XMLListCollection(rpcEvent.result.pfiles.item);
			sendNotification(FFIAApplicationFacade.LOAD_PFILES_BY_REFERENCE_SUCCESS, proxyName, projectVO.project_id);
		}
		
		public function failedLoadProjectFilesByReference(rpcEvent:Object):void {
			errorStatus = "Could not load reference files!";
			sendNotification(FFIAApplicationFacade.LOAD_PFILES_BY_REFERENCE_FAILED, errorStatus, projectVO.project_id);
		}
		
		public function get pfilesDP():XMLListCollection {
			return pfiles;
		}
		
		public function newProjectFiles():XML {
			var xml:XML = new XML("<item><pfilesproject>" + projectVO.project_id  +   "</pfilesproject></item>");
			return xml;
		}
		
		public function removeProjectFiles(selIndex:int):Object {
			var file:Object = pfilesDP.removeItemAt(selIndex);
			var delegate:RemoveProjectFileDelegate = new RemoveProjectFileDelegate(
				function(rcpEvent:Object) {
			
				},
				function(rcpEvent:Object) {
			
				});
			delegate.removeProjectFile(file.pfile_id);
			return file;
		}
		
		public function copyProjectFiles(projectVO:Object, copyItem:Object, targetRow:int):Object {
			var item:XML = new XML(XML(copyItem).toXMLString());
			item['pfilesproject'] = String(projectVO.project_id);
			item['pfilesid'] = '';
			if (targetRow == pfilesDP.length - 1) {
				pfilesDP.addItemAt(item, targetRow);
			} else {
				pfilesDP.setItemAt(item, targetRow);
			}
			return item;
		}
		
		public function saveProjectFiles(file:Object):void {
			var delegate:SaveProjectFileDelegate = new SaveProjectFileDelegate(resultSaveProjectFiles, faultSaveProjectFiles);
			delegate.save(file);
		}
		
		public function resultSaveProjectFiles(rcpEvent:Object):void {
			var success:Boolean = rcpEvent.result.success == 'true'; 
			if (success) {
				sendNotification(FFIAApplicationFacade.PROJECT_ITEM_UPDATED, projectVO, projectVO.project_id);
			} else {
				RIALogger.error(this, "Save quotation lines failed with error message: \n" + rcpEvent.result.errors);
				sendNotification(FFIAApplicationFacade.PROJECT_ITEM_UPDATE_FAILED, rcpEvent.result.errors, projectVO.project_id);				
			}
		}
		
		public function faultSaveProjectFiles(rcpEvent:Object):void {
			errorStatus = "Something wrong when save quotation lines!";
			sendNotification(FFIAApplicationFacade.PROJECT_UPDATE_FAILED, errorStatus, projectVO.project_id);
		}
	}
}